package wxapp

import (
	"wxsdk/wxpay/core"
	. "wxsdk/wxpay"
)

type CloseOrderRequest struct {
	Appid        string   `xml:"appid"`                //小程序ID
	Mch_id       string   `xml:"mch_id"`               //商户号
	SubAppid     string   `xml:"sub_appid,omitempty"`  //小程序的APPID,当前调起支付的小程序APPID(服务商特有参数)
	SubMchId     string   `xml:"sub_mch_id,omitempty"` //微信支付分配的子商户号(服务商特有参数)
	OutTradeNo   string   `xml:"out_trade_no"`         //商户订单号
	NonceStr     string   `xml:"nonce_str"`            //随机字符串
	Sign         string   `xml:"sign"`                 //签名
	SignType     string   `xml:"sign_type,omitempty"`  //签名类型
	RequestXML   string   `xml:"-"`                    //最终请求串
}

// 关闭订单
type CloseOrderResponse struct {
	BaseResponse `xml:",innerXml,omitempty"`
}

func  CloseOrder(appId,mchId,subAppid,subMchId,appSecret,orderIdStr string) (CloseOrderResponse,error){
	client := core.GetWxPayClient(appId,mchId,appSecret,nil)
	var rsp CloseOrderResponse
	var req = new(CloseOrderRequest)
	if len(subAppid) > 0 {
	   req.SubAppid = subAppid
	}
	if len(subMchId) > 0 {
	   req.SubMchId = subMchId
	}
	req.OutTradeNo = orderIdStr         // 商户订单号
	err := client.Excute(CloseOrderUrl, req, &rsp)
    if err != nil {
		return rsp, err
	}
	return rsp, nil
}
